package academic.ui.client.animation;


import academic.ui.client.widgets.FadePanel;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.user.client.DOM;

/**
 * An {@link Animation} used to open the content.
 */
public class FadeAnimation extends Animation {
  /**
   * Whether the item is being opened or closed.
   */
	
  public FadeAnimation()
  {
	  
  }
  
  private int ANIMATION_DURATION = 2600;
  
  
  private boolean isOpening;

  private FadePanel curPanel;

  /**
   * Open or close the content.
   * 
   * @param panel the panel to open or close
   * @param animate true to animate, false to open instantly
   */
  public void setOpen(FadePanel panel, boolean animate) {
    // Immediately complete previous open
    cancel();

    // Open the new item
    if (animate) {
      curPanel = panel;
      isOpening = panel.isOpen;
      run(ANIMATION_DURATION);
    } else {
      panel.contentWrapper.setVisible(panel.isOpen);
      if (panel.isOpen) {
        // Special treatment on the visible case to ensure LazyPanel works
        panel.getContent().setVisible(true);
      }
    }
  }

  @Override
  protected void onComplete() {
    if (!isOpening) {
      curPanel.contentWrapper.setVisible(false);
    }
    DOM.setStyleAttribute(curPanel.contentWrapper.getElement(), "opacity",
        "1");
    curPanel = null;
  }
  

  @Override
  protected void onStart() {
    super.onStart();
    if (isOpening) {
      curPanel.contentWrapper.setVisible(true);
      // Special treatment on the visible case to ensure LazyPanel works
      curPanel.getContent().setVisible(true);
    }
  }

  @Override
  protected void onUpdate(double progress) {
    double opacity = DOM.getElementPropertyInt(
        curPanel.contentWrapper.getElement(), "opacity");
  
    if (!isOpening) {
  	  opacity = 1 - progress;
    }
    else
  	  opacity=progress;
    
    DOM.setStyleAttribute(curPanel.contentWrapper.getElement(), "opacity",
       String.valueOf(opacity));
    double IEOpacity = opacity*100;
    DOM.setStyleAttribute(curPanel.contentWrapper.getElement(), "filter", "alpha(opacity = "+String.valueOf(IEOpacity)+")");
  }

public void setAnimationTime(int time) {
	ANIMATION_DURATION=time;
}
}